package com.qian2.pricesharingapplication.shoppinglist;

import java.util.ArrayList;

import java.util.HashMap;

import android.util.Log;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SqliteController  extends SQLiteOpenHelper {
	private static final String LOGCAT = null;

	public SqliteController(Context applicationcontext) {
        super(applicationcontext, "androidsqlite.db", null, 1);
        Log.d(LOGCAT,"Created");
    }
	
	@Override
	public void onCreate(SQLiteDatabase database) {
		String query;
		query = "CREATE TABLE ShoppingList ( ItemId INTEGER PRIMARY KEY, ItemName TEXT, Bought INTEGER)";
        database.execSQL(query);
        Log.d(LOGCAT,"ShoppingList Created");
	}
	@Override
	public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
		String query;
		query = "DROP TABLE IF EXISTS ShoppingList";
		database.execSQL(query);
        onCreate(database);
	}
	
	public void insertItem(HashMap<String, Object> queryValues) {
		SQLiteDatabase database = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("Bought", (Integer) 0);
		values.put("ItemName", (String) queryValues.get("ItemName"));
		database.insert("ShoppingList", null, values);
		database.close();
	}
	
	public void insertItem_undo(HashMap<String, Object> queryValues) {
		SQLiteDatabase database = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("Bought", (Integer) queryValues.get("Bought"));
		values.put("ItemId", (String) queryValues.get("ItemId"));
		values.put("ItemName", (String) queryValues.get("ItemName"));
		database.insert("ShoppingList", null, values);
		database.close();
	}
	
	
	public void removeItem(HashMap<String, Object> queryValues) {
		SQLiteDatabase database = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ItemId", (String) queryValues.get("ItemId"));
		System.out.println(((String) queryValues.get("ItemId")).length());
		database.delete("ShoppingList", "ItemId="+queryValues.get("ItemId"), null );
		database.close();
	}
	
	public void removeItem(String name) {
		SQLiteDatabase database = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("ItemId", (String) check_item_id(name));
		System.out.println((check_item_id(name).length()));
		database.delete("ShoppingList", "ItemId="+check_item_id(name), null );
		database.close();
	}
	
	public int updateShoppingList(HashMap<String, Object> queryValues) {
		SQLiteDatabase database = this.getWritableDatabase();	 
	    ContentValues values = new ContentValues();
	    values.put("ItemName", (String) queryValues.get("ItemName"));
		values.put("Bought", (Integer) queryValues.get("Bought"));
	    return database.update("ShoppingList", values, "ItemId" + " = ?", new String[] { (String) queryValues.get("ItemId") });	    
	}
	
	public ArrayList<HashMap<String, Object>> getAllItems() {
		ArrayList<HashMap<String, Object>> wordList;
		wordList = new ArrayList<HashMap<String, Object>>();
		String selectQuery = "SELECT  * FROM ShoppingList ORDER BY ItemId DESC";
	    SQLiteDatabase database = this.getWritableDatabase();
	    Cursor cursor = database.rawQuery(selectQuery, null);
	    if (cursor.moveToFirst()) {
	        do {
	        	HashMap<String, Object> map = new HashMap<String, Object>();
	        	map.put("ItemId", cursor.getString(0));
	        	map.put("ItemName", cursor.getString(1));
	        	map.put("Bought", cursor.getInt(2));
                wordList.add(map);
	        } while (cursor.moveToNext());
	    }
	 
	    // return contact list
	    return wordList;
	}
	
	public boolean check_item_exist(String name) {
		SQLiteDatabase database = this.getReadableDatabase();
		String selectQuery = "SELECT * FROM ShoppingList where ItemName='"+name+"'";
		Cursor cursor = database.rawQuery(selectQuery, null);
		if (cursor.moveToFirst()) {
	    	return true;
	    }	
		else return false;

	}	
	
	public String check_item_id(String name) {
		SQLiteDatabase database = this.getReadableDatabase();
		String selectQuery = "SELECT * FROM ShoppingList where ItemName='"+name+"'";
		Cursor cursor = database.rawQuery(selectQuery, null);
		if (cursor.moveToFirst()) {
					HashMap<String, String> map = new HashMap<String, String>();
	        	return cursor.getString(0);
	    }	
		return null;

	}	
}
